<?php

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Entity\User;

class HomeController extends AbstractActionController
{
    protected $_objectManager;
    
    public function showAction()
    {   
        $users = $this->getObjectManager()->getRepository('\Application\Model\Entity\User')->findAll();
        $projects = $this->getObjectManager()->getRepository('\Application\Model\Entity\Project')->findAll();
        return new ViewModel(array(
            'users'     => $users,
            'projects'  => $projects
                    )
                );
    }
    
    public function addProjectAction()
    {
        if ($this->request->isPost()) {
            $project = new \Application\Model\Entity\Project();
            $project->setTitle($this->getRequest()->getPost('title'));
            $project->setDescription($this->getRequest()->getPost('description'));
            $project->setDescription(1);
            $project->setShortdescription($this->getRequest()->getPost('shortDescription'));
            $user = $this->getObjectManager()->find('\Application\Model\Entity\User', 1);
            $project->setUsers($user);
            
            $category = $this->getObjectManager()->find('\Application\Model\Entity\Category', 1);
            $project->setCategory($category);
            
            $status = $this->getObjectManager()->find('\Application\Model\Entity\Status', 1);
            $project->setStatuses($status);            
            
            $this->getObjectManager()->persist($project);
            $this->getObjectManager()->flush();
            $newId = $project->getId();

            return $this->redirect()->toRoute('user/wildcard', array('controller' => 'home', 'action' => 'show'));
        }
        
        return new ViewModel();
    }
    
    
    
    public function loginAction()
    {
        if ($this->request->isPost()) {
            
            $password = $this->getRequest()->getPost('password');
            $email = $this->getRequest()->getPost('email');
             
            $user = $this->getObjectManager()->getRepository('\Application\Model\Entity\User')->findBy(array('email' => $email, 'password' => $password));

           
             if(empty($user)){
                 return new ViewModel(array('resp' => 'Wprowadzony niprawidłowy login lub hasło'));                
             }
             else{
                 return $this->redirect()->toRoute('user/wildcard', array('controller' => 'home', 'action' => 'show'));    
             }
        }
        
        return new ViewModel();
        
    }
    
    public function showProjectAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $project = $this->getObjectManager()->find('\Application\Model\Entity\Project', $id);

        return new ViewModel(array('project' => $project));
    }
    
    public function allProjectsAction()
    {
        $projects = $this->getObjectManager()->getRepository('\Application\Model\Entity\Project')->findAll();
        return new ViewModel(array('projects' => $projects));
    }
    
    public function addAction()
    {
        if ($this->request->isPost()) {
            $user = new User();
            $user->setFirstname($this->getRequest()->getPost('firstname'));
            $user->setLastname($this->getRequest()->getPost('lastname'));


            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();
            $newId = $user->getId();

            return $this->redirect()->toRoute('user/wildcard', array('controller' => 'home', 'action' => 'show'));
        }
        
        return new ViewModel();
    }
    
    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id');
        $user = $this->getObjectManager()->find('\Application\Model\Entity\User', $id);
        
        if ($this->request->isPost()) {
            $user->setFirstName($this->getRequest()->getPost('firstname'));
            $user->setLastName($this->getRequest()->getPost('lastname'));           
            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('user/wildcard', array('controller' => 'home', 'action' => 'show'));
        }

        return new ViewModel(array('user' => $user));
    }
    
    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Application\Model\Entity\User', $id);

        if ($this->request->isPost()) {
            $this->getObjectManager()->remove($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('user/wildcard', array('controller' => 'home', 'action' => 'show'));
        }

        return new ViewModel(array('user' => $user));
    }
    
    protected function getObjectManager()
    {
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }
}
